package me.vudu.Conflict.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import me.vudu.Conflict.ConflictPlugin;
import org.bukkit.Bukkit;
import org.getspout.spoutapi.player.SpoutPlayer;

public class ConflictLogger {

	public static Logger logger;
	public ConflictPlugin plugin;

	public static ConflictLogger instance;
	
	public HashMap<String, HashMap<Long, Integer>> playerToMapRed;
	public HashMap<String, HashMap<Long, Integer>> playerToMapBlue;
	public ArrayList<Long> timestamps;
	

	public ConflictLogger() {

		playerToMapRed = new HashMap<String, HashMap<Long,Integer>>();
		playerToMapBlue = new HashMap<String, HashMap<Long,Integer>>();
		timestamps = new ArrayList<Long>(500);
		
		try {
			logger = Logger.getLogger("");
			FileHandler fh = new FileHandler("plugins/Conflict/logData.log", true);
			fh.setFormatter(new LoggFormatter());
			logger.removeHandler(logger.getHandlers()[0]);
			logger.addHandler(fh);
			
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static ConflictLogger getLogger() {
		if (null == instance) {
			instance = new ConflictLogger();
		}
		return instance;
	}
	
	

	public void logTimestamp() {
		long timestamp = Bukkit.getServer().getWorlds().get(0).getFullTime();
		timestamp = timestamp / 20; // Ticks->Sec.
		int hours = (int) timestamp / 3600;
		int min = (int) (timestamp % 3600) / 60;
		int sec = (int) (timestamp % 60);
		String s_hours = hours < 10 ? "0" + Integer.toString(hours) : Integer.toString(hours);
		String s_min = min < 10 ? "0" + Integer.toString(min) : Integer.toString(min);
		String s_sec = sec < 10 ? "0" + Integer.toString(sec) : Integer.toString(sec);

		logger.log(Level.INFO,
				"Timestamp: " + s_hours + ":" + s_min + ":" + s_sec + System.getProperty("line.separator"));

	}
	
	public void addXP(){
		long timestamp = Bukkit.getServer().getWorlds().get(0).getFullTime()/20;
		timestamps.add(timestamp);
		for (SpoutPlayer sp : ConflictPlugin.players) {
			if(!playerToMapRed.containsKey(sp.getName()) && !playerToMapBlue.containsKey(sp.getName())){
				HashMap<Long, Integer> values = new HashMap<Long, Integer>();
				
				if(ConflictPlugin.playersRed.contains(sp)){
					playerToMapRed.put(sp.getName(), values);	
				}else{
					playerToMapBlue.put(sp.getName(), values);
				}
				
			}
			if(playerToMapRed.containsKey(sp.getName())){
				(playerToMapRed.get(sp.getName())).put(timestamp, ConflictPlugin.totalXP.get(sp.getName()));	
			}else{
				(playerToMapBlue.get(sp.getName())).put(timestamp, ConflictPlugin.totalXP.get(sp.getName()));
			}
			
			
		}
		
		
		
	}

	public void logXP() {
		
		

		logger.log(Level.INFO, "XP"+System.getProperty("line.separator") );
		logger.log(Level.INFO, "RED,");
		for(String s: playerToMapRed.keySet()){
			logger.log(Level.INFO, s + "," );
			HashMap<Long, Integer> theMap = playerToMapRed.get(s);
			for(long ts: timestamps){
				int xp = 0;
				if(null!= theMap.get(ts)){
					xp = theMap.get(ts);
				}
				logger.log(Level.INFO, xp + "," );
			}
			logger.log(Level.INFO, System.getProperty("line.separator") );
		}
		logger.log(Level.INFO, "BLUE,");
		for(String s: playerToMapBlue.keySet()){
			logger.log(Level.INFO, s + "," );
			HashMap<Long, Integer> theMap = playerToMapBlue.get(s);
			for(long ts: timestamps){
				int xp = 0;
				if(null!= theMap.get(ts)){
					xp = theMap.get(ts);
				}
				logger.log(Level.INFO, xp + "," );
			}
			logger.log(Level.INFO, System.getProperty("line.separator") );
		}
		logger.log(Level.INFO, System.getProperty("line.separator"));
	}

	public void logSkills() {
		logger.log(Level.INFO, "Skills:" + System.getProperty("line.separator"));
		logger.log(Level.INFO, "RED:" + System.getProperty("line.separator"));
		for (String name : playerToMapRed.keySet()) {
			logger.log(Level.INFO, name+","+ConflictPlugin.totalSkills.get(name)+",");
		}
		logger.log(Level.INFO, System.getProperty("line.separator"));
		logger.log(Level.INFO, "BLUE:" + System.getProperty("line.separator"));
		for (String name : playerToMapBlue.keySet()) {
			logger.log(Level.INFO, name+","+ConflictPlugin.totalSkills.get(name)+",");
		}
		logger.log(Level.INFO, System.getProperty("line.separator"));

	}
	
	public void logGeneric(String name, HashMap<String, Integer> values) {
		logger.log(Level.INFO, name+":" + System.getProperty("line.separator"));
		logger.log(Level.INFO, "RED:" + System.getProperty("line.separator"));
		int sum=0;
	
		
		for (String pname : playerToMapRed.keySet()) {
			logger.log(Level.INFO, pname+","+Integer.toString(values.get(pname))+",");
			sum+=values.get(pname);
		}
		logger.log(Level.INFO, "Sum,"+sum+ System.getProperty("line.separator"));
		sum=0;
		logger.log(Level.INFO, "BLUE:" + System.getProperty("line.separator"));
		for (String pname : playerToMapBlue.keySet()) {
			logger.log(Level.INFO, pname+","+Integer.toString(values.get(pname))+",");
			sum+=values.get(pname);
		}
		logger.log(Level.INFO, "Sum,"+sum+ System.getProperty("line.separator"));
		
		
	}

}
